package com.zhuchaooasystem.ui.common;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.presenter.common.ImagePagerContract;
import com.zhuchaooasystem.presenter.common.ImagePagerPresenter;
import com.zhuchaooasystem.utils.ScreenUtils;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.HackyViewPager;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ImagePagerActivity extends BaseActivity<ImagePagerPresenter> implements ImagePagerContract.View {
    @BindView(R.id.actionbar_relative_rootview)
    RelativeLayout relative_rootview;
    @BindView(R.id.actionbar_txt_title)
    TextView txt_title;
    @BindView(R.id.actionbar_iv_back)
    ImageView iv_back;
    //----
    @BindView(R.id.act_image_pager_hacky_images)
    HackyViewPager hacky_images;
    @BindView(R.id.act_image_pager_scroll_content)
    ScrollView scroll_content;
    @BindView(R.id.act_image_pager_txt_content)
    TextView txt_content;

    //-----
    private List<String> list_images;
    private int position; //当前要显示的图片位置（默认为0：第一张）
    private boolean isCache; //图片加载是否需要缓存
    private String content; //要显示的内容

    @Override
    protected int getLayout() {
        return R.layout.activity_image_pager;
    }

    @Override
    protected ImagePagerPresenter getPresenter() {
        return new ImagePagerPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        if (null != getIntent() && null != getIntent().getExtras()) {
            Bundle bundle = getIntent().getExtras();
            list_images = bundle.getStringArrayList("images");
            isCache = bundle.getBoolean("isCache", true);
            position = bundle.getInt("position", 0);
            content = bundle.getString("content", "");
            //----
            if (!(null != list_images && list_images.size() > 0)) {
                ToastUtil.showShort(mContext, "当前没有可显示的图片");
                finish();
            }
        } else {
            finish();
        }
    }

    @Override
    protected void initView() {
        setActionBar();
        //----
        if (!TextUtils.isEmpty(content)) {
            scroll_content.setVisibility(View.VISIBLE);
            txt_content.setText(content);
        } else {
            scroll_content.setVisibility(View.GONE);
        }
        //----
        initRecycle();
    }

    private void initRecycle() {
        //----
        hacky_images.setAdapter(new ImagePagerAdapter());
        hacky_images.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                txt_title.setText((position + 1) + "/" + list_images.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        hacky_images.setCurrentItem(position);
    }

    private void setActionBar() {
        //----菜单栏背景变透明
        relative_rootview.setBackgroundColor(getResources().getColor(R.color.color_77000000));
        txt_title.setTextColor(Color.WHITE);
        txt_title.setText((position + 1) + "/" + list_images.size());
        iv_back.setImageResource(R.drawable.ic_back_circle);
        iv_back.setPadding(ScreenUtils.dip2px(mContext, 10), ScreenUtils.dip2px(mContext, 10), ScreenUtils.dip2px(mContext, 10), ScreenUtils.dip2px(mContext, 10));
        iv_back.setVisibility(View.VISIBLE);

    }

    @Override
    protected void initDataAndLoadData() {
    }

    @OnClick({R.id.actionbar_iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.actionbar_iv_back:
                finish();
                break;
        }
    }

    class ImagePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return null == list_images ? 0 : list_images.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = null;
            try {
                photoView = new PhotoView(container.getContext());
                //----
                if (!isCache) {
                    Glide.with(mContext).load(list_images.get(position))
                            .asBitmap().error(R.drawable.ic_image_default).diskCacheStrategy(DiskCacheStrategy.NONE).into(photoView);
                } else {
                    Glide.with(mContext).load(list_images.get(position)).asBitmap().error(R.drawable.ic_image_default).into(photoView);
                }
                //----
                photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                                                    @Override
                                                    public void onPhotoTap(View view, float x, float y) {
                                                        if (!TextUtils.isEmpty(content)) {
                                                            if (scroll_content.getVisibility() == View.VISIBLE) {
                                                                scroll_content.startAnimation(AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.photo_dialog_out_anim));
                                                                scroll_content.setVisibility(View.GONE);
                                                            } else {
                                                                scroll_content.startAnimation(AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.photo_dialog_in_anim));
                                                                scroll_content.setVisibility(View.VISIBLE);
                                                            }
                                                        } else {
                                                            scroll_content.setVisibility(View.GONE);
                                                        }
                                                    }
                                                }

                );
                //----
                container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            } catch (Exception e) {
            }
            return photoView;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            try {
                //----
                container.removeView((View) object);
            } catch (Exception e) {
            }
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

}
